import { getTotalKey, getFactoryTotalKey } from '@/api/dashboard'

const getDefaultState = () => {
  return {
    lincenseStatistics: {
		total:0, 		//License总数
		available:0,		//License可用数
		invalid:0			//License无效数
	}
  }
}

const state = getDefaultState()

const mutations = {
  SET_LICENSE_NUM: (state, lincenseStatistics) => {
	let {tot, tot1, tot2} = lincenseStatistics
    state.lincenseStatistics.total = tot;
    state.lincenseStatistics.available = tot1;
    state.lincenseStatistics.invalid = tot2;
  }
  
}

const actions = {
	// 获取license统计
	
	getLicense({ commit, state },roles) {
	  return new Promise((resolve, reject) => {
		if(roles == 'admin' || roles == 'hichs'){
			getTotalKey().then(res => {
			  const license = res
			  commit('SET_LICENSE_NUM', license)
			  resolve(license)
			}).catch(error => {
			  reject(error)
			})
		}else if(roles == 'factory' ){
			getFactoryTotalKey().then(res => {
			  const license = res.data
			  commit('SET_LICENSE_NUM', license)
			  resolve(license)
			}).catch(error => {
			  reject(error)
			})
		}
		
	  })
	},
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}